import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/single_histogram_chart_painter.dart';
import 'package:x3_charts_core/chart_tools/chart_tools.dart';
import 'package:x3_charts_core/model/single_histogram_chart_widget_model.dart';

///**
///* @author hrongyong
///* @date   2/24/21
///* @email hrongyong@gmail.com
///* Description: 单个直方图图表
///*/
class SingleHistogramChartWidget extends StatefulWidget {
  ///图表数组
  final List<SingleHistogramChartWidgetModel> chartList;

  ///Y轴最大值
  final double yAxisMax;

  ///背景颜色
  final Color backgroundColor;

  ///x轴线颜色
  final Color xAxisColor;

  ///x轴线描述颜色
  final Color xAxisDescriptionColor;

  ///y轴线颜色
  final Color yAxisColor;

  ///y轴线描述颜色
  final Color yAxisDescriptionColor;

  ///绘制X轴线
  final bool drawXAxis;

  ///绘制X轴线刻度
  final drawXAxisScale;

  ///绘制Y轴线
  final bool drawYAxis;

  ///绘制Y轴线刻度
  final drawYAxisScale;

  ///直方图填充颜色
  final Color histogramFillColor;

  SingleHistogramChartWidget({
    @required this.chartList,
    @required this.yAxisMax,
    @required this.histogramFillColor,
    this.backgroundColor = Colors.white,
    this.xAxisColor = Colors.black12,
    this.yAxisColor = Colors.black12,
    this.xAxisDescriptionColor = Colors.black,
    this.yAxisDescriptionColor = Colors.black,
    this.drawXAxis = true,
    this.drawXAxisScale = true,
    this.drawYAxis = false,
    this.drawYAxisScale = true,
  });

  @override
  _SingleHistogramChartWidgetState createState() => _SingleHistogramChartWidgetState();
}

class _SingleHistogramChartWidgetState extends State<SingleHistogramChartWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: this,
    )..forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return CustomPaint(
            size: Size(constraints.maxWidth, constraints.maxHeight),
            painter: SingleHistogramChartPainter(
              yAxisMax:  ChartTools.calculateTheMaximum(widget.yAxisMax).toInt(),
              chartList: widget.chartList,
              repaint: _controller,
              histogramFillColor: widget.histogramFillColor,
              backgroundColor: widget.backgroundColor,
              xAxisColor: widget.xAxisColor,
              xAxisDescriptionColor: widget.xAxisDescriptionColor,
              yAxisColor: widget.yAxisColor,
              yAxisDescriptionColor: widget.yAxisDescriptionColor,
              drawXAxis: widget.drawXAxis,
              drawXAxisScale: widget.drawXAxisScale,
              drawYAxis: widget.drawYAxis,
              drawYAxisScale: widget.drawYAxisScale,
            ),
          );
        },
      ),
    );
  }
}
